package com.wulian.user.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.wulian.common.dto.CommonDataDto;
import com.wulian.common.entity.SysUser;
import com.wulian.common.util.DateUtil;
import com.wulian.user.dto.QueryLogOutWatchWarningDto;
import com.wulian.user.dto.ResultBulletinBoardLogOutWatchWarningDto;
import com.wulian.user.entity.LogOutWatchWarning;
import com.wulian.user.mapper.LogOutWatchWarningMapper;
import com.wulian.user.service.LogOutWatchWarningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 *@Description:()
 * @author  caifulin
 * @date  2020/6/16 13:50
 * @version 1.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class LogOutWatchWarningServiceImpl implements LogOutWatchWarningService {
    @Autowired
    private LogOutWatchWarningMapper logOutWatchWarningMapper;

    @Override
    public List<LogOutWatchWarning> logOutNum(SysUser sysUser,String type) {
        String leader = sysUser.getLeader();
        QueryLogOutWatchWarningDto queryLogOutWatchWarningDto = new QueryLogOutWatchWarningDto();
        Date start = null;
        Date end = null;

        queryLogOutWatchWarningDto.setProjectId(sysUser.getProjectId());
        if(null == leader || leader.equals("1") ){//是否时领导层0：是 1 不是
            queryLogOutWatchWarningDto.setRegisterName(sysUser.getUserId());
        }
        if(null != type && type.equals("week")){//一周为维度
            start = DateUtil.getWeekDateStart();
            end = DateUtil.getToDateEnd();
        }else{
            Map<String, Date> date = DateUtil.getDate("0");
            start = date.get("start");
            end = date.get("end");
        }
        queryLogOutWatchWarningDto.setStartDate(start);
        queryLogOutWatchWarningDto.setEndDate(end);
        List<LogOutWatchWarning> logOutWatchWarnings = logOutWatchWarningMapper.logOutNumList(queryLogOutWatchWarningDto);
        return logOutWatchWarnings;
    }

    @Override
    public List<CommonDataDto>  bulletinBoardlogOutNumByPage(Page page, SysUser sysUser) {
        QueryLogOutWatchWarningDto queryLogOutWatchWarningDto = new QueryLogOutWatchWarningDto();
        queryLogOutWatchWarningDto.setProjectId(sysUser.getProjectId());
        String leader = sysUser.getLeader();
        if(null == leader || leader.equals("1") ){//是否时领导层0：是 1 不是
            queryLogOutWatchWarningDto.setRegisterName(sysUser.getUserId());
        }
        List<ResultBulletinBoardLogOutWatchWarningDto> resultBulletinBoardLogOutWatchWarningDtos = logOutWatchWarningMapper.bulletinBoardlogOutNumByPage(page, queryLogOutWatchWarningDto);
        Map<String, List<ResultBulletinBoardLogOutWatchWarningDto>> collect = resultBulletinBoardLogOutWatchWarningDtos.stream().collect(Collectors.groupingBy(ResultBulletinBoardLogOutWatchWarningDto::getGroupDate));

        /*Map<String,List<ResultBulletinBoardLogOutWatchWarningDto>> result = new TreeMap<>();
        collect.entrySet().stream()
                .sorted(Map.Entry.<String,List<ResultBulletinBoardLogOutWatchWarningDto>>comparingByKey().reversed())
                .forEachOrdered(x->result.put(x.getKey(),x.getValue()));*/
        Set<String> keySet = collect.keySet();
        Set<String> sortSet = new TreeSet<String>(Comparator.reverseOrder());
        sortSet.addAll(keySet);
        List<CommonDataDto> list = new ArrayList<CommonDataDto>();
        for(String key: sortSet){
            List<ResultBulletinBoardLogOutWatchWarningDto> resultBulletinBoardFenceWarningInfoPages1 = collect.get(key);
            CommonDataDto commonDataDto = new CommonDataDto();
            commonDataDto.setKey(key);
            commonDataDto.setList(resultBulletinBoardFenceWarningInfoPages1);
            list.add(commonDataDto);
        }
        return list;
    }
}